package queue;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ClassName Main
 * @Description 使用生产者测试者测试效果
 * @Company inspur
 * @Author Kevin
 * @Date 2020/7/10 17:10
 * @Version 1.0
 */
public class Main {
    public static void main(String[] args) {
        MyBlockingQueue<Integer> queue = new MyBlockingQueue<>(10);
        ExecutorService consumers = Executors.newFixedThreadPool(10);
        ExecutorService produces = Executors.newFixedThreadPool(10);
        Thread consumer = new Thread(() -> {
            for (int i = 0; i < 500; i++) {
                consumers.execute(()->{
                    int result = queue.take();
                    System.out.println("consumer:" + Thread.currentThread().getName() + Thread.currentThread().getId() + Thread.currentThread().getState() + " take Result: " + result);
                });
            }
        });
        Thread produce = new Thread(()->{
            for (int i = 0; i < 500; i++) {
                int finalI = i;
                produces.execute(()->{
                    queue.put(finalI);
                    System.out.println("produce:" + Thread.currentThread().getName() + Thread.currentThread().getId() + Thread.currentThread().getState()+ " put Result: " + finalI);
                });
            }
        });
        produce.start();
        consumer.start();
    }
}
